/*大转盘*/

$(".rotate-con-zhen").rotate({
	bind: {
		click: function() {
			var currentUser = AV.User.current();
			if(currentUser) {
				if(myvue.rotate_num < 3) {
					var a = runzp();
					$(this).rotate({
						duration: 3000, //转动时间
						angle: 0, //起始角度
						animateTo: 1440 + a.angle, //结束的角度
						easing: $.easing.easeOutSine, //动画效果，需加载jquery.easing.min.js
						callback: function() {
							myvue.yhj();
							if(a.prize != "") {
								myvue.Coupon.push(parseInt(a.prize));
								myvue.setyhj();
							}
							alert(a.prize + a.message); //简单的弹出获奖信息
						}
					});
				} else {
					alert("每个用户只能参加3次哦");
					return false;
				}
			} else {
				var a = confirm("猴哥提醒您登陆才能参加活动");
				if(a == true) {
					location.href = "../login/login.html";
				}
			}
		}
	}
});

/*限时抢购*/
function checktime(i) {
	if(i < 10) {
		i = "0" + i;

	} else {
		i = i;
	}
	return i;
}

function leftTime() {
	var endTime = new Date("2017/6/6,12:00:00"); //结束时间
	var curTime = new Date(); //当前时间
	var leftTime = parseInt((endTime.getTime() - curTime.getTime()) / 1000); //获得时间差
	//小时、分、秒需要取模运算
	//	var d = parseInt(leftTime / (60 * 60 * 24));
	var h = parseInt(leftTime / (60 * 60) % 24);
	var m = parseInt(leftTime / 60 % 60);
	var s = parseInt(leftTime % 60);
	var ms = parseInt(((endTime.getTime() - curTime.getTime()) / 100) % 10);
	//	var txt = h+"小时"+m+"分钟"+s+"."+ms+"秒";
	//	$(".ontimeBuy_title_right").html(txt);
	$(".hh").html(h < 10 ? ("0" + h) : h);
	$(".mm").html(m < 10 ? ("0" + m) : m);
	$(".ss").html(s < 10 ? ("0" + s) : s);
	if(leftTime <= 0) {
		$(".ontimeBuy_title_right").html("团购结束");
		clearInterval(set);
	}
};
leftTime();
var set = setInterval(leftTime, 100);

var myvue = new Vue({
	el: "body",
	data: {
		rotate_num: "",
		userid: "",
		Coupon: [],
		productdata: ""
	},
	created: function() {
		this.leancloudinit();
	},
	methods: {
		leancloudinit: function() {
			var _this = this;
			var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
			var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
			AV.init({
				appId: APP_ID,
				appKey: APP_KEY
			});
			var currentUser = AV.User.current();
			if(currentUser) {
				var phonenum = AV.User.current().attributes.username;
				var query = new AV.Query('ELM_user');
				query.contains('phonenum', phonenum);
				query.find().then(function(data) {
					_this.userid = data[0].id;
					_this.rotate_num = data[0].attributes.rotate_num;
					_this.Coupon = eval(data[0].attributes.Coupon);
				}, function(error) {
					// 异常处理
				})
			}
			var query = new AV.Query('ELM_cheaper');
			query.find().then(function(data) {
				_this.productdata = data;
			}, function(error) {
				// 异常处理
			});
		},
		yhj: function() {
			var userid = this.userid;
			this.rotate_num = (this.rotate_num * 1 + 1).toString();
			var rotate_num = this.rotate_num;
			var todo = AV.Object.createWithoutData('ELM_user', userid);
			// 修改属性
			todo.set('rotate_num', rotate_num);
			// 保存到云端
			todo.save()
		},
		setyhj: function() {
			var userid = this.userid;
			var Coupon = this.Coupon;
			var todo = AV.Object.createWithoutData('ELM_user', userid);
			// 修改属性
			todo.set('Coupon', "[" + Coupon + "]");
			// 保存到云端
			todo.save()
		},
		//在这里添加到订单
		tzcheap: function(index) {
			var _this = this;
			var currentUser = AV.User.current();
			if(!currentUser) {
				var a = confirm("必须登陆后才能参加限时抢购");
				if(a == true) {
					location.href = "../login/login.html";
				}
			} else {
				var nowtime = new Date().toLocaleString();
				var phonenum = AV.User.current().attributes.username;
				var username = "";
				var userdata = new AV.Query('ELM_user');
				userdata.contains('phonenum', phonenum);
				userdata.find().then(function(data) {
					username = data[0].attributes.username;
					var Product = AV.Object.extend('ELM_order');
					var product = new Product();
					product.set('if_pay', false);
					product.set('shopid', _this.productdata[index].attributes.shopid);
					product.set('shopimg', _this.productdata[index].attributes.shopimg);
					product.set('buy_time', nowtime);
					product.set('shop_sendprice', _this.productdata[index].attributes.sendprice);
					product.set('username', username);
					product.set('user_phonenum', phonenum);
					product.set('shop_name', _this.productdata[index].attributes.shopname);
					product.set('total', 1);
					product.set('totle', _this.productdata[index].attributes.productsdata[0].price);
					product.set('products_data', _this.productdata[index].attributes.productsdata);
					product.set('user_adress', data[0].attributes.adress);
					product.save().then(function(data) {
						//  发布成功
						location.href = "../order/order.html?orderid=" + seturl(data.id);
					}, function(error) {
						alert("添加订单失败");
					});
				}, function(error) {
					// 异常处理
				});
			}
		}
	}
})